﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace CSF.Utility.Security
{
    public class DESHelper
    {
        public static byte[] EncryptDES(string encryptString, byte[] encryptKey)
        {
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(encryptKey, encryptKey), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();

            return mStream.ToArray();
        }

        public static string DecryptDES(byte[] decryptBytes, byte[] encryptKey)
        {

            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(encryptKey, encryptKey), CryptoStreamMode.Write);
            cStream.Write(decryptBytes, 0, decryptBytes.Length);
            cStream.FlushFinalBlock();

            return Encoding.UTF8.GetString(mStream.ToArray());
        }
    }
}
